package com.dom.invoapp.modular.provider;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dom.api.basic.BasicSysCodeApi;
import com.dom.common.enums.SysCodeTblEnum;
import com.dom.common.model.vo.basic.B209A;
import com.dom.common.model.vo.basic.B209R;
import com.dom.common.model.vo.basic.SysCodeInfo;
import com.dom.invoapp.dao.entity.TSysCodeTbl;
import com.dom.invoapp.dao.service.ITSysCodeTblService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * @Description 基础信息管理
 * @Author xllei
 * @Date 2018/11/14 下午3:00
 * @Version 1.0
 **/
@RestController
public class BasicSysCodeProvider implements BasicSysCodeApi {
    @Autowired
    private ITSysCodeTblService sysCodeTblService;

    @Override
    public B209A getPurcOrderStatList(B209R request) {
        return findSysCode(SysCodeTblEnum.SCT_PURC_ORDER_STAT);
    }

    @Override
    public B209A getVerfyStatList(B209R request) {
        return findSysCode(SysCodeTblEnum.SCT_VERFY_STAT);
    }

    private B209A findSysCode(SysCodeTblEnum sysCodeTblEnum){
        List<SysCodeInfo> sysCodeInfos = getSysCodeInfoList(sysCodeTblEnum.getCode());


        B209A response = new B209A();
        response.setSysCodeInfos(sysCodeInfos);
        response.setTotalNum(sysCodeInfos == null ? 0 : sysCodeInfos.size());
        return response;
    }

    private List<TSysCodeTbl> getSysCodeList(int sysId){
        Objects.requireNonNull(sysId);
        Wrapper<TSysCodeTbl> wrapper = new EntityWrapper<>();
        wrapper.eq("sys_code", sysId);

        return sysCodeTblService.selectList(wrapper);
    }

    private List<SysCodeInfo> getSysCodeInfoList(int sysId){
        List<SysCodeInfo> results = new ArrayList<>();
        List<TSysCodeTbl> records = getSysCodeList(sysId);
        records.forEach(entity->{
            SysCodeInfo sysCodeInfo = new SysCodeInfo();
            BeanUtils.copyProperties(entity, sysCodeInfo);
            results.add(sysCodeInfo);
        });
        return results;
    }
}
